package com.jic.veteran.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.jic.veteran.common.api.CommonPage;
import com.jic.veteran.common.api.CommonResult;
import com.jic.veteran.dto.ProjectBasicQueryParam;
import com.jic.veteran.dto.ProjectDto;
import com.jic.veteran.model.ProjectBasic;
import com.jic.veteran.service.ProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "ProjectController")
@Controller
@RequestMapping("/project")
public class ProjectController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ProjectController.class);

    @Autowired
    private ProjectService projectService;

    @ApiOperation("新建项目")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody ProjectDto projectDto) {
        int count = projectService.create(projectDto.getProjectBasic(), projectDto.getProjectImplement());
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("查询所有项目基本信息列表")
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<ProjectBasic>> listAll() {
        List<ProjectBasic> projectBasicList = projectService.listAll();
        return CommonResult.success(projectBasicList);
    }

    @ApiOperation("查询项目基本信息列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<ProjectBasic>> list(ProjectBasicQueryParam projectBasicQueryParam, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<ProjectBasic> projectBasicList = projectService.list(projectBasicQueryParam, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(projectBasicList));
    }

    @ApiOperation("修改项目信息")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable Long id, @RequestBody ProjectDto projectDto) {
        int count = projectService.update(id, projectDto);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("根据项目详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<ProjectDto> getItem(@PathVariable Long id) {
        ProjectDto projectDto = projectService.getItem(id);
        if (ObjectUtil.isEmpty(projectDto)) {
            return CommonResult.failed("项目信息不存在");
        }
        return CommonResult.success(projectDto);
    }

    @ApiOperation("发布项目")
    @RequestMapping(value = "/release/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult release(@PathVariable Long id) {
        int count = projectService.release(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("停止项目")
    @RequestMapping(value = "/stop/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult stop(@PathVariable Long id) {
        int count = projectService.stop(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("删除项目")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable Long id) {
        int count = projectService.stop(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("项目分析")
    @RequestMapping(value = "/stat", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<JSONObject> stat(@RequestParam(value = "startDate") String startDate,
                                         @RequestParam(value = "endDate") String endDate) {
        JSONObject jsonObject = projectService.getDonateData(startDate,endDate);
        return CommonResult.success(jsonObject);
    }
}
